package com.example.zack.cyrano;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;


public class MyActivity extends Activity {

    private ArrayList<String> listItems = new ArrayList<String>();
    private ArrayAdapter<String> adapter;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_my);
        listView = (ListView) findViewById(R.id.listView);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                System.out.println("Dix");
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        Intent intent = new Intent(this, SettingsActivity.class);
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        String contactGroup = "";
//        for (String phone : data.getStringArrayListExtra("phones")) {
//            listItems.add(phone);
//        }
        HashMap<String, String> contacts = (HashMap<String, String>) data.getSerializableExtra("contacts");
        int count = 0;
        for (String key : contacts.keySet()) {
            if (count < contacts.keySet().size()-1) {
                contactGroup += contacts.get(key) + ", ";
            } else {
                contactGroup += contacts.get(key);
            }
            count++;
        }

        if (!contactGroup.isEmpty()) {
            listItems.add(contactGroup);
            adapter.notifyDataSetChanged();
        }

    }

    public void sendMessage(View view) {
        Intent intent = new Intent(this, MultiContactsPicker.class);
        startActivityForResult(intent, 1);
    }
}
